********************************************************************************************
**** GENERATE NEW VARIABLES FOR DEMAND SYSTEM
**** This file generates new variables and save the compact database for the analysis.
**** Version: 16/04/2014
********************************************************************************************
	
***********************************************************
**** 
**** ADJUST VARIABLES FOR PANEL 2011
**** 
***********************************************************
	
	* School variables and age for 2011
		
		recode q2_27 (2 = 0)
		recode q2_32 (2 = 0)
		recode q2_04a (2 = 0)
		
		gen temp = q2_27 if round==2012
		bys id: egen max = max(temp)		
		replace q2_05 = max if round==2011
		replace q2_04a = max if round==2010
		drop max temp
		
		gen temp = q2_28level if round==2012
		bys id: egen max = max(temp)		
		replace q2_06level = max if round==2011
		drop max temp
		
		gen temp = q2_28class if round==2012
		bys id: egen max = max(temp)		
		replace q2_06class = max if round==2011
		drop max temp	

		gen temp = q2_32 if round==2012
		bys id: egen max = max(temp)		
		replace q2_10 = max if round==2011
		drop max temp	
				
******************************************************************
**** Age int and age at baseline
******************************************************************

	gen age_int = round(age)
	label var age_int "Age"
	
	gen age_base = age_int if round == 2010
	bys id: egen max = max(age_base)
	replace age_base = max 
	drop max

	replace age_base = age_int - 2 if age_base == . & round == 2012
	
	* Generate age for 2011
	
		gen temp = age_base if round == 2012
		bys id: egen temp2 = max(temp)
		replace age_base = temp2 if round == 2011
		drop temp temp2
		
		foreach var of varlist age_int age {
		
			gen temp = `var' if round == 2012
			bys id: egen temp2 = max(temp)
			replace `var' = temp2 - 1 if round == 2011
			drop temp temp2
	
		}
		
	replace age_base = . if age_base < 0 & (round == 2011 | round == 2012)
	replace age_int = . if age_base < 0 & round == 2011
	replace age = . if age_base < 0 & round == 2011
		
***********************************************************
**** 
**** GENERATE NEW VARIABLES
**** 
***********************************************************

	* Religion
	
		gen religion = q1_41 if q1_05 == 1
		
		replace religion = 3 if (religion == 4 | religion == 5)
	
		bys samplecode: egen max = max(religion)
		replace religion = max
		drop max
	
		label define religion 1 "Orthodox" 2 "Islam" 3 "Other", add
		label values religion religion
	
		gen rel_muslim = (religion == 2) & !mi(religion)
		label var rel_muslim "Muslim"
	
		gen rel_other = (religion == 3) & !mi(religion)
		label var rel_other "Other religion"
		
		
	* Generate new dummies

		gen asset_quantile_1 = (asset_quantile==1) if !mi(asset_quantile)
		label var asset_quantile_1 "Wealth tercile (1)"

		gen asset_quantile_2 = (asset_quantile==2) if !mi(asset_quantile)
		label var asset_quantile_2 "Wealth tercile (2)"

		gen asset_quantile_3 = (asset_quantile==3) if !mi(asset_quantile)
		label var asset_quantile_3 "Wealth tercile (3)"

		gen members_2 = (members == 2) if !mi(members)
		label var members_2 "Household members (2 members)"

		gen members_3 = (members == 3) if !mi(members)
		label var members_3 "Household members (3 members)"

		gen members_4 = (members == 4) if !mi(members)
		label var members_4 "Household members (4 members)"

		gen members_5 = (members >= 5) if !mi(members)
		label var members_5 "Household members (5 or more members)"


		gen members_13_18_1 = (members_13_18==1) if !mi(members_13_18)
		label var members_13_18_1 "Household members 13-18 (1)"

		gen members_13_18_2 = (members_13_18==2) if !mi(members_13_18)
		label var members_13_18_2 "Household members 13-18 (2)"

		gen members_13_18_3 = (members_13_18>=3) if !mi(members_13_18)
		label var members_13_18_3 "Household members 13-18 (3 or more)"
		
***********************************************************
**** 
**** GENERATE MINIMUM DISTANCE FROM SWC 
**** 
***********************************************************

	gen min_dist_SWC = t_distance_1
	label var min_dist_SWC "(Log-)Distance from SWC" 
	
	gen min_dist_SWC2 = t_distance_1
	label var min_dist_SWC2 "(Log-)Distance from SWC (second)" 
	
	foreach var of varlist t_distance_1 t_distance_2 t_distance_3 t_distance_4 t_distance_5 t_distance_6 t_distance_7 t_distance_8 t_distance_9 t_distance_10 t_distance_11 t_distance_12 t_distance_13 t_distance_14 t_distance_15 t_distance_16 t_distance_17 t_distance_18 t_distance_19 t_distance_20 t_distance_21 t_distance_22 t_distance_23 t_distance_24 t_distance_25 t_distance_26 t_distance_27 t_distance_28 t_distance_29 t_distance_30 t_distance_31 t_distance_32 t_distance_33 t_distance_34 t_distance_35 t_distance_36 t_distance_37  {
		
		replace min_dist_SWC = `var' if `var'< min_dist_SWC & !mi(`var')
		replace min_dist_SWC2 = `var' if `var'< min_dist_SWC2 & `var'> min_dist_SWC & !mi(`var')
		
	}
	
	gen min_time_SWC = t_time_1
	label var min_time_SWC "(Log-)Time from SWC" 
	
	
	foreach var of varlist t_time_1 t_time_2 t_time_3 t_time_4 t_time_5 t_time_6 t_time_7 t_time_8 t_time_9 t_time_10 t_time_11 t_time_12 t_time_13 t_time_14 t_time_15 t_time_16 t_time_17 t_time_18 t_time_19 t_time_20 t_time_21 t_time_22 t_time_23 t_time_24 t_time_25 t_time_26 t_time_27 t_time_28 t_time_29 t_time_30 t_time_31 t_time_32 t_time_33 t_time_34 t_time_35 t_time_36 t_time_37  {
		
		replace min_time_SWC = `var' if `var'< min_time_SWC & !mi(`var')
		
	}
	
	replace min_dist_SWC = ln(min_dist_SWC)
	replace min_time_SWC = ln(min_time_SWC)
	
		
***********************************************************
**** 
**** MEMBERS
**** 
***********************************************************

	* Adult members
	
		gen male_adults = (age > 18 & gender == 1 & ((q1_04a != 4 & q1_04a != 3) | q1_04a==.)) 
		bys round samplecode: egen sum = sum(male_adults)
		replace male_adults = sum
		drop sum
	
	* Memebers 16-20 at follow up
		
			gen memb = (age>=16 & age<=20) if (!mi(age) & round==2012)

			bys round samplecode: egen sum = sum(memb)
			replace memb = sum 
			drop sum
	
			gen memb_0 = (memb==0) & !mi(memb)
			label var memb_0 "No child 16-20"
			gen memb_1 = (memb==1) & !mi(memb)
			label var memb_1 "One child 16-20"
			gen memb_2 = (memb==2) & !mi(memb)
			label var memb_2 "Two children 16-20"
			gen memb_3 = (memb>=3) & !mi(memb)
			label var memb_3 "Three or more chidren 16-20"
			
	* Generate members 15-18 
		
		drop members_15_18
		
		ge aux = 0 if age_int != .
		replace aux = 1 if age_int >= 15 & age_int <= 18
		bys samplecode round: egen members_15_18 = sum(aux)
		label var members_15_18 "Members 15-18 years old"
		drop aux

		ge aux = 0 if age_int != .
		replace aux = 1 if age_int >= 14.5 & age_int <= 19
		bys samplecode round: egen members_15_18_b = sum(aux)
		label var members_15_18_b "Members 15-18 years old"
		drop aux
		
	* Generate members 14-19 for each semester
		
		ge aux = 0 if age != .
		replace aux = 1 if age >= 14 & age < 14.5
		bys samplecode round: egen members_14_14_5 = sum(aux)
		label var members_14_14_5 "Members 14-14.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 14.5 & age < 15
		bys samplecode round: egen members_14_5_15 = sum(aux)
		label var members_14_5_15 "Members 14.5-15 years old"
		drop aux
		
		ge aux = 0 if age != .
		replace aux = 1 if age >= 15 & age < 15.5
		bys samplecode round: egen members_15_15_5 = sum(aux)
		label var members_15_15_5 "Members 15-15.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 15.5 & age < 16
		bys samplecode round: egen members_15_5_16 = sum(aux)
		label var members_15_5_16 "Members 15.5-16 years old"
		drop aux
			
		
		ge aux = 0 if age != .
		replace aux = 1 if age >= 16 & age < 16.5
		bys samplecode round: egen members_16_16_5 = sum(aux)
		label var members_16_16_5 "Members 16-16.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 16.5 & age < 17
		bys samplecode round: egen members_16_5_17 = sum(aux)
		label var members_16_5_17 "Members 16.5-17 years old"
		drop aux
			
		ge aux = 0 if age != .
		replace aux = 1 if age >= 17 & age < 17.5
		bys samplecode round: egen members_17_17_5 = sum(aux)
		label var members_17_17_5 "Members 17-17.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 17.5 & age < 18
		bys samplecode round: egen members_17_5_18 = sum(aux)
		label var members_17_5_18 "Members 17.5-18 years old"
		drop aux
		
		ge aux = 0 if age != .
		replace aux = 1 if age >= 18 & age < 18.5
		bys samplecode round: egen members_18_18_5 = sum(aux)
		label var members_18_18_5 "Members 18-18.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 18.5 & age < 19
		bys samplecode round: egen members_18_5_19 = sum(aux)
		label var members_18_5_19 "Members 18.5-19 years old"
		drop aux
		
		ge aux = 0 if age != .
		replace aux = 1 if age >= 19 & age < 19.5
		bys samplecode round: egen members_19_19_5 = sum(aux)
		label var members_19_19_5 "Members 19-19.5 years old"
		drop aux

		ge aux = 0 if age != .
		replace aux = 1 if age >= 19.5 & age < 20
		bys samplecode round: egen members_19_5_20 = sum(aux)
		label var members_19_5_20 "Members 19.5-20 years old"
		drop aux

***********************************************************
**** 
**** AGE CATEGORIES
**** 
***********************************************************
		
		* SMALL groups
	
			gen age_int_cat_s = .
			global skip_s = 3  // determine the age group size (small groups)
		
				forvalues x = 0/6 {
		
					local min = 6 + $skip_s * `x'
					local max = 6 + $skip_s * (`x' + 1)
			
					replace age_int_cat_s = `x' if (age_int >= `min' & age_int < `max') & !mi(age)
					label define age_int_cat_s `x' "`min'-`max'", add
				
				}

				label values age_int_cat_s age_int_cat_s

		* LARGE groups
		
			gen age_int_cat_l = .
			global skip_l = 6  // determine the age group size (large groups)

				forvalues x = 0/3 {
		
					local min = 6 + $skip_l * `x'
					local max = 6 + $skip_l * (`x' + 1)
			
					replace age_int_cat_l = `x' if (age_int >= `min' & age_int < `max') & !mi(age)
					label define age_int_cat_l `x' "`min'-`max'", add
				
				}

				label values age_int_cat_l age_int_cat_l

		* Generate members by age categories for children
	
			foreach num of numlist 10 12 14 16 18 20 22 24 {
	
				local max = `num' + 2
				gen count_d = (age >= `num' & age < `max')
				bys samplecode round: egen children_`num' = sum(count_d)
				label var children_`num' "Number of children (`num'-`max' y.o.)"
				drop count_d
	
			}
			
		* Generate age categories for household head (d_age_head_l35 d_age_head_35 d_age_head_40 d_age_head_45 d_age_head_50 d_age_head_55)
	
			gen d_age_head_l35 = (age_head < 35) if !mi(age_head)
			label var d_age_head_l35 "Age head (less than 35 y.o.)"
			
	
			foreach num of numlist 35 40 45 50 {
	
				local max = `num' + 5
				gen d_age_head_`num' = (age_head >= `num' & age_head < `max') if !mi(age_head)
				label var d_age_head_`num' "Age head (`num'- `max' y.o.)"	
			}
			
			gen d_age_head_55 = (age_head >= 55) if !mi(age_head)
			label var d_age_head_55 "Age head (more than 55 y.o.)"


***********************************************************
**** 
**** OTHER VARIABLES
**** 
***********************************************************

	* Generate time dummy

		gen d_2012 = (round == 2012)
		label var d_2012 "2012"
		
		gen d_2011 = (round == 2011)
		label var d_2011 "2011"

		
	* Re-label variables for tables
	
		label var ln_expenditure_adj "Log Monthly consumption"
		label var ln_expenditure "Log Monthly expenditure"

		
	* Generate correct income variables 
		
		gen fam_income_h =  q8_01_02 if gender == 1
		replace fam_income_h = q8_01_03 if gender == 0 
		label var fam_income_h "Income received as assistance from husband's family and friends (no debt)"  
	
		gen fam_income_w =  q8_01_02 if gender == 0
		replace fam_income_w = q8_01_03 if gender == 1
		label var fam_income_w "Income received as assistance from wife's family and friends (no debt)"    
	
		gen fam_debt_h =  q8_01_04 if gender == 1
		replace fam_debt_h = q8_01_05 if gender == 0 
		label var fam_debt_h "Income received as assistance from husband's family and friends (debt)"  
	
		gen fam_debt_w =  q8_01_04 if gender == 0
		replace fam_debt_w = q8_01_05 if gender == 1
		label var fam_debt_w "Income received as assistance from wife's family and friends (debt)"    			


***********************************************************
**** 
**** VARIABLES ABOUT SCHOOLING
**** 
*********************************************************** 
	
	* Generate enrolment variables
	
		ge enrolled_prim = q2_05
		replace enrolled_prim = 0 if enrolled_prim != . & q2_06level >= 6 
		replace enrolled_prim = . if enrolled_prim == 1 & mi(q2_06level)
		label var enrolled_prim "Enrolled in primary school"

		ge enrolled_sec = q2_05
		replace enrolled_sec = 0 if enrolled_sec != . & (q2_06level != 6 & q2_06level != 7 & q2_06level != 8)
		replace enrolled_sec = . if enrolled_sec == 1 & mi(q2_06level)
		label var enrolled_sec "Enrolled in secondary school"
		
		ge enrolled_uni = q2_05
		replace enrolled_uni = 0 if enrolled_uni != . & q2_06level <= 8
		replace enrolled_uni = . if enrolled_uni == 1 & mi(q2_06level)
		replace enrolled_uni = 0 if q2_06level == 13
		label var enrolled_uni "Enrolled in university"
	
	* Generate municipality balance
	
		gen temp = enrolled_sec if (round == 2010 & age_base>=15 & age_base<=19 & gender==1)
		bys id_mun: egen mean_enrolled_sec = mean(temp)
		label var mean_enrolled_sec "Average enrolment in secondary (boys, 15-19)"
		drop temp
				
	
	* Generate other school variables
	
		gen missed_0 = (q2_11 == 1) if !mi(q2_11)
		label var missed_0 "Never missed school"

		gen missed_21 = (q2_11 == 4 | q2_11 == 5) if !mi(q2_11)
		label var missed_21 "Missed 21 or more days "
		
	* Finished secondary school 	

		gen finished_two = ((q2_04a == 0 & ((q2_04level == 8 & q2_04class==4) | (q2_04level == 7 & q2_04class==3)| (q2_04level == 6 & q2_04class==2) | (q2_04level >8 & q2_04level!=13))) | (q2_04a == 1 & (q2_04level >8 & q2_04level!=13))) if (!mi(q2_04level) & !mi(q2_04class))
		label var finished_two "Finished secondary school"
		
***********************************************************
**** 
**** GENERATE SECOND STAGE EXPENDITURE SHARES FOR FOOD
**** 
***********************************************************	
				
	foreach var of varlist sh_food_cereal sh_food_veg sh_food_meat sh_food_fish sh_food_oil sh_food_dairy sh_food_ttea sh_food_ss sh_food_choc sh_food_coffee sh_food_beverages sh_food_outside sh_food_other {
	
			gen s`var' = `var' / sh_food
			local lab: variable label `var'
        	label var s`var' "`lab'"
        	
        }
    
    foreach var of varlist sh_food_cereal_adj sh_food_veg_adj sh_food_meat_adj sh_food_fish_adj sh_food_oil_adj sh_food_dairy_adj sh_food_ttea_adj sh_food_ss_adj sh_food_choc_adj sh_food_coffee_adj sh_food_beverages_adj sh_food_outside_adj sh_food_other_adj {
	
			gen s`var' = `var' / sh_food_adj
			local lab: variable label `var'
        	label var s`var' "`lab'"
        	
        }    
        
***********************************************************
**** 
**** AGGREGATE FOOD CONSUMPTION
**** 
***********************************************************	
       
  		* Meat, fish and dairy
 
			gen exp_food_mfd = exp_food_meat + exp_food_fish + exp_food_dairy
			gen sh_food_mfd = sh_food_meat + sh_food_fish + sh_food_dairy
			gen sh_food_mfd_adj = sh_food_meat_adj + sh_food_fish_adj + sh_food_dairy_adj
			gen ssh_food_mfd = ssh_food_meat + ssh_food_fish + ssh_food_dairy
			gen ssh_food_mfd_adj = ssh_food_meat_adj + ssh_food_fish_adj + ssh_food_dairy_adj
			gen nzero_food_mfd = (exp_food_mfd > 0) if !mi(exp_food_mfd)
			
				foreach var of varlist exp_food_mfd sh_food_mfd sh_food_mfd_adj ssh_food_mfd ssh_food_mfd_adj nzero_food_mfd {
			
					label var `var' "Meat, fish and dairy"
				
				}
		
		* Turkish tea, Coffe and tea and beverages _ttea _coffee _beverages
		
			gen exp_food_ctb = exp_food_ttea + exp_food_coffee + exp_food_beverages
			gen sh_food_ctb = sh_food_ttea + sh_food_coffee + sh_food_beverages
			gen sh_food_ctb_adj = sh_food_ttea_adj + sh_food_coffee_adj + sh_food_beverages_adj
			gen ssh_food_ctb = ssh_food_ttea + ssh_food_coffee + ssh_food_beverages
			gen ssh_food_ctb_adj = ssh_food_ttea_adj + ssh_food_coffee_adj + ssh_food_beverages_adj
			gen nzero_food_ctb = (exp_food_ctb > 0) if !mi(exp_food_ctb)
			
				foreach var of varlist exp_food_ctb sh_food_ctb sh_food_ctb ssh_food_ctb ssh_food_ctb nzero_food_ctb {
			
					label var `var' "Coffe, tea and beverages"
				
				}

		* Sugar, honey, salt and chocolate and sweets
		
			gen exp_food_scs = exp_food_ss + exp_food_choc
			gen sh_food_scs = sh_food_ss + sh_food_choc
			gen sh_food_scs_adj = sh_food_ss_adj + sh_food_choc_adj
			gen ssh_food_scs = ssh_food_ss + ssh_food_choc
			gen ssh_food_scs_adj = ssh_food_ss_adj + ssh_food_choc_adj
			gen nzero_food_scs = (exp_food_scs > 0) if !mi(exp_food_scs)
			
				foreach var of varlist exp_food_scs sh_food_scs sh_food_scs_adj ssh_food_scs ssh_food_scs_adj nzero_food_scs {
			
					label var `var' "Sweets and salt"
				
				}

		* Meals outside the household and other food
		
			gen exp_food_o = exp_food_outside + exp_food_other
			gen sh_food_o = sh_food_outside + sh_food_other
			gen sh_food_o_adj = sh_food_outside_adj + sh_food_other_adj
			gen ssh_food_o = ssh_food_outside + ssh_food_other
			gen ssh_food_o_adj = ssh_food_outside_adj + ssh_food_other_adj
			gen nzero_food_o = (exp_food_o > 0) if !mi(exp_food_o)
			
				foreach var of varlist exp_food_o sh_food_o sh_food_o_adj ssh_food_o ssh_food_o_adj nzero_food_o {
			
					label var `var' "Other food"
				
				}

***********************************************************
**** 
**** AGGREGATE OTHER EXPENDITURES AND CONSUMPTION
**** 
***********************************************************	

	* UTILITIES (add home cleaning)
	
			replace exp_utilities = exp_utilities + exp_home
			replace sh_utilities = sh_utilities + sh_home
			replace sh_utilities_adj = sh_utilities_adj + sh_home_adj
			replace sh_pur_utilities_adj = sh_pur_utilities_adj + sh_pur_home_adj
			replace sh_sf_utilities_adj = sh_sf_utilities_adj + sh_sf_home_adj
			replace sh_stor_utilities_adj = sh_stor_utilities_adj + sh_stor_home_adj
			replace sh_gift_utilities_adj = sh_gift_utilities_adj + sh_gift_home_adj			
			replace nzero_utilities = (sh_utilities > 0) if sh_utilities!=.
	
	* HEALTH (add hygiene) 
	
			replace exp_health = exp_health + exp_hygiene
			replace sh_health = sh_health + sh_hygiene
			replace sh_health_adj = sh_health_adj + sh_hygiene_adj
			replace sh_pur_health_adj = sh_pur_health_adj + sh_pur_hygiene_adj
			replace sh_sf_health_adj = sh_sf_health_adj + sh_sf_hygiene_adj
			replace sh_stor_health_adj = sh_stor_health_adj + sh_stor_hygiene_adj
			replace sh_gift_health_adj = sh_gift_health_adj + sh_gift_hygiene_adj			
			replace nzero_health = (sh_health > 0) if sh_health!=.

		* Split equally expenditure on hygiene
			
				*sh_health_w
				*sh_health_h
				*sh_health_m_ch 
				*sh_health_f_ch 
				*sh_health_ch   
				*sh_health_other 
				
		* Generate adults versus children
		
	* ENTERTAINMENT TO OTHER GOODS
	
			replace exp_other = exp_other + exp_entertainment
			replace sh_other = sh_other + sh_entertainment
			replace sh_other_adj = sh_other_adj + sh_entertainment_adj
			replace nzero_other = (sh_other > 0) if sh_other!=.

***********************************************************
**** 
**** GENERATE HEAD INFORMATION AT BASELINE
**** 
***********************************************************	
	
*	foreach var of varlist no_educ_head low_primary_head sec_school_head more_sec_head gender_head d_age_head_l35 d_age_head_35 d_age_head_40 d_age_head_50 d_age_head_55 {
*	
*		gen temp = `var' if round == 2010
*		bys samplecode: egen temp2 = max(temp)
*		replace `var' = temp2 if round == 2012 & !mi(temp2)
*		drop temp temp2
*	
*	}

***********************************************************
**** 
**** GENERATE ADDITIONAL DURABLES
**** 
***********************************************************
 
	egen a1 = rmax(dur_fuel_cooker dur_fuel_stove)
	label var a1 "Solid fuel cooker or Solid fuel/oil stove"
	
	egen a2 = rmax(dur_electric_cooker dur_electric_stove)
	label var a2 "Electric/gas cooker or Electric stove"
	
	egen a3 = rmax(dur_electric_cooker dur_electric_stove dur_gas_stove)
	label var a3 "Electric/gas cooker or Electric stove or Gas stove"
	
	egen a4 = rmax(dur_motor dur_car dur_trailer dur_other_vehicles)
	label var a4 "Motorbike, car, trailer or other vehicles"
	
***********************************************************
**** 
**** REPORT VARIABLES AT HOUSEHOLD LEVEL
**** 
***********************************************************	
	
		* Report household level data at individual level
	
		foreach var of varlist q15_01 $Expenditure_var $Expenditure_shares_var $Expenditure_adj_shares_var $Durables_var $Utilities_var $Income_var {		
			bys samplecode round: egen max = max(`var')
			replace `var' = max
			drop max
		}

	* Determine variables at household level for each round about SFA status
	
		 gen sfa_2012 = (q17_a_filter_main==1) if !mi(q17_a_filter_main)
		 label var sfa_2012 "SFA recipient in 2012"

		 gen sfa_2011 = (q17_b_filter_main==1) if !mi(q17_b_filter_main)
		 label var sfa_2011 "SFA recipient in 2011"
		 
 		 gen sfa_2010 = (q17_c_filter_main==1) if !mi(q17_c_filter_main)
		 label var sfa_2010 "SFA recipient in 2010"
			
		foreach var of varlist sfa_2012 sfa_2011 sfa_2010 {		
			bys samplecode : egen max = max(`var')
			replace `var' = max
			drop max
		}

	* Endogenous treatment variable
	
		gen mother_t = ((mother == 0 & (gender_head == 0 & cct_treat_hh == 1)) | (mother == 1 & cct_treat_hh == 1))
		label var mother_t "Payment to mother"
		
***********************************************************
**** 
**** CHECK FOR PRESENCE OF BOTH PARENTS
**** 
***********************************************************

		
	gen m_present = (q1_05 == 1 | q1_05 == 2) & gender == 1
	gen f_present = (q1_05 == 1 | q1_05 == 2) & gender == 0

	foreach var of varlist m_present f_present {

		bys samplecode round: egen max = max(`var')
		replace `var' = max
		drop max
		
	}
	
	gen A1A2 = m_present == 1 & f_present == 1
	label var A1A2 "Both partners present"
	
***********************************************************
**** 
**** GENERATE MEAN REGRESSORS AT BASELINE
**** 
***********************************************************

	foreach var of varlist $Expenditure_shares_var {
	
		bys id_mun: egen meanmun_`var' = mean(`var') if round == 2010
		bys id_mun: egen temp = max(meanmun_`var')
		replace meanmun_`var' = temp
		drop temp
	
	}
	

***********************************************************
**** 
**** LABELING VARIABLES
**** 
***********************************************************

	label var electricity_always "Access to electricity"
	label var public_water_always "Access to public water" 
	label var telephone_always "Access to telephone line"
	label var toilet_sewerage "Access to sewerage"
	label var nonshared_toilet "Non-shared toiled"

	label var dwelling_house "Type of dwelling (house)" 
	label var dwelling_flat "Type of dwelling (flat)"
	label var dwelling_shared "Type of dwelling (rooms in shared flat)"
	label var dwelling_otherstructure "Type of dwelling (rooms in other structure)"
	label var dwelling_other "Type of dwelling (other dwelling)"

	label var property_owner "Type of property (owner)"
	label var property_holder_state "Type of property (leaseholder of state property)"
	label var property_holder_private "Type of property (leaseholder of private property)" 
	label var property_holder_partprivate "Type of property (leaseholder of part of dwelling in private property)"  
	label var property_relative "Type of property (relative's property)"
	label var property_other "Type of property (other type)"

* Monitoring

	label var reports_never "Check school diaries (never)"
	label var reports_monthly "Check school diaries (monthly)"
		replace reports_monthly = 1 if (reports_semester == 1 | reports_year == 1)
	label var reports_semester "Check school diaries (once per semester)"
		replace reports_semester = 1 if (reports_year == 1)
	label var reports_year "Check school diaries (yearly)"

	label var parent_never "Going to parental meetings (never)"
	label var parent_1 "Going to parental meetings (once per year)"
	label var parent_2 "Going to parental meetings (twice per year)"
		replace parent_2 = 1 if (parent_1 == 1)
	label var parent_3 "Going to parental meetings (three times per year)"
		replace parent_3 = 1 if (parent_2 == 1 | parent_1 == 1)
	label var parent_4 "Going to parental meetings (four times or more per year)"
		replace parent_4 = 1 if (parent_3 == 1 | parent_2 == 1 | parent_1 == 1)

	label var asking_never "Talk to children about school (never)"
	label var asking_day "Talk to children about school (daily)"
		replace asking_day = 1 if (asking_week==1 | asking_month == 1)
	label var asking_week "Talk to children about school (weekly)"
		replace asking_week = 1 if (asking_month == 1)
	label var asking_month "Talk to children about school (monthly)"
		
	* Recorde self reported attendance using cumulative answers

		label var missed_never "Self reported attendance (missed at least 1 day of school)"
		label var missed_1_10  "Self reported attendance (missed at least 11 day of school)"
		label var missed_11_20 "Self reported attendance (missed at least 21 day of school)"
		label var missed_21_40 "Self reported attendance (missed at least 41 day of school)"
	
		replace missed_never = 0 if (missed_never == 1) & !mi(missed_never)
		replace missed_never = 1 if (missed_1_10==1 | missed_11_20 == 1 | missed_21_40 == 1 | missed_40 == 1) & !mi(missed_never)
	
		replace missed_1_10 = 0 if (missed_never == 1 | missed_1_10==1) & !mi(missed_1_10)
		replace missed_1_10 = 1 if (missed_11_20 == 1 | missed_21_40 == 1 | missed_40 == 1) & !mi(missed_1_10)
	
		replace missed_11_20 = 0 if (missed_never == 1 | missed_1_10==1 | missed_11_20 == 1) & !mi(missed_11_20)
		replace missed_11_20 = 1 if (missed_21_40 == 1 | missed_40 == 1) & !mi(missed_11_20)
		
		replace missed_21_40 = 0 if (missed_never == 1 | missed_1_10==1 | missed_21_40 == 1) & !mi(missed_21_40)
		replace missed_21_40 = 1 if (missed_40 == 1) & !mi(missed_21_40)
	
	
	label var transp_school "Transportation to school (school transport)"
	label var transp_public "Transportation to school (public transport)"
	label var transp_priv_mot "Transportation to school (private motorized)"
	label var transp_priv_n_mot "Transportation to school (private non-motorized)"
	label var transp_walk "Transportation to school (walking)"
	
	* Print table labels for subgroups 

		foreach v of varlist exp_food_cereal exp_food_veg exp_food_meat exp_food_fish exp_food_oil exp_food_dairy exp_food_ttea exp_food_ss exp_food_choc exp_food_coffee exp_food_beverages exp_food_outside exp_food_other exp_w_clothing exp_h_clothing exp_ch_clothing exp_school_m exp_school_f exp_school_m_0_14 exp_school_f_0_14 exp_school_m_15 exp_school_f_15 exp_health_w exp_health_h exp_health_ch exp_health_other {
		
			label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
			
		}

		
***********************************************************
**** 
**** Generate dummy for welfare center presence in the
**** municipality
**** 
***********************************************************		
		
		gen welfare_center = 0
		label var welfare_center "SWC in municipality"
		replace welfare_center = 1 if id_mun == 3  // Berovo
		replace welfare_center = 1 if id_mun == 4  // Bitola
		replace welfare_center = 1 if id_mun == 9  // Butel
		replace welfare_center = 1 if id_mun == 79 // Cair
		replace welfare_center = 1 if id_mun == 77 // Centar
		replace welfare_center = 1 if id_mun == 21 // Debar
		replace welfare_center = 1 if id_mun == 23 // Delcevo
		replace welfare_center = 1 if id_mun == 25 // Demir Hisar
		replace welfare_center = 1 if id_mun == 17 // Gazi Baba
		replace welfare_center = 1 if id_mun == 18 // Gevgelija
		replace welfare_center = 1 if id_mun == 19 // Gostivar
		replace welfare_center = 1 if id_mun == 38 // Karpos
		replace welfare_center = 1 if id_mun == 36 // Kavadarci
		replace welfare_center = 1 if id_mun == 40 // Kicevo
		replace welfare_center = 1 if id_mun == 39 // Kisela Voda
		replace welfare_center = 1 if id_mun == 42 // Kocani
		replace welfare_center = 1 if id_mun == 44 // Kriva Palanka
		replace welfare_center = 1 if id_mun == 46 // Krusevo
		replace welfare_center = 1 if id_mun == 47 // Kumanovo
		replace welfare_center = 1 if id_mun == 48 // Lipkovo
		replace welfare_center = 1 if id_mun == 52 // Makedonski Brod
		replace welfare_center = 1 if id_mun == 54 // Negotino
		replace welfare_center = 1 if id_mun == 58 // Ohrid
		replace welfare_center = 1 if id_mun == 62 // Prilep
		replace welfare_center = 1 if id_mun == 63 // Probistip
		replace welfare_center = 1 if id_mun == 64 // Radovis
		replace welfare_center = 1 if id_mun == 66 // Resen
		replace welfare_center = 1 if id_mun == 83 // Stip
		replace welfare_center = 1 if id_mun == 72 // Struga
		replace welfare_center = 1 if id_mun == 73 // Strumica
		replace welfare_center = 1 if id_mun == 84 // Suto Orizari
		replace welfare_center = 1 if id_mun == 69 // Sveti Nikole
		replace welfare_center = 1 if id_mun == 76  // Tetovo
		replace welfare_center = 1 if id_mun == 10  // Valandovo
		replace welfare_center = 1 if id_mun == 13  // Veles
		replace welfare_center = 1 if id_mun == 14  // Vinica
				
***********************************************************
**** 
**** Generate variables for time use
**** 
***********************************************************
	
	foreach var of varlist sh_sleeping sh_chores sh_working sh_elderly sh_shopping sh_leisure_ch sh_leisure_no_ch sh_study_ch sh_other_ch sh_other_no_ch {
	
		* Men
		
		gen `var'_m = `var' if gender == 1
		
			bys samplecode round: egen temp = max(`var'_m)
			replace `var'_m = temp
			drop temp
	
		* Women
		
			gen `var'_f = `var' if gender == 0
		
				bys samplecode round: egen temp = max(`var'_f)
				replace `var'_f = temp
				drop temp
		
	}

		label var sh_sleeping_m "Sleeping"
		label var sh_chores_m "House chores"
		label var sh_working_m "Working"
		label var sh_elderly_m "Care of elderly"
		label var sh_shopping_m "Shopping"
		label var sh_leisure_ch_m "Leisure with children"
		label var sh_leisure_no_ch_m "Leisure without children"
		label var sh_study_ch_m "Study with children"
		label var sh_other_ch_m "Other with children"
		label var sh_other_no_ch_m "Other without children"

		label var sh_sleeping_f "Sleeping"
		label var sh_chores_f "House chores"
		label var sh_working_f "Working"
		label var sh_elderly_f "Care of elderly"
		label var sh_shopping_f "Shopping"
		label var sh_leisure_ch_f "Leisure with children"
		label var sh_leisure_no_ch_f "Leisure without children"
		label var sh_study_ch_f "Study with children"
		label var sh_other_ch_f "Other with children"
		label var sh_other_no_ch_f "Other without children"

***********************************************************
**** 
**** CHILDREN IN SCHOOL
**** 
***********************************************************
	
	foreach var of varlist finished_two enrolled_prim enrolled_sec enrolled_uni {
	
		bys samplecode round: egen m_`var' = mean(`var') if age<22 & sfa == 1
		bys samplecode round: egen max = max(m_`var')
		replace m_`var' = max
		drop max
		
		bys samplecode round: egen sum_`var' = sum(`var') if sfa == 1
		bys samplecode round: egen max = max(sum_`var')
		replace sum_`var' = max
		drop max
		
	}
		
		
		bys id_mun: egen mean_corr = mean(enrolled_sec) if round == 2010 & sfa == 1 & age>15 & age<19
		bys id_mun: egen max = max(mean_corr)
		replace mean_corr = max
		drop max
		
		bys id_mun: egen mean_ill = mean(ill) if round == 2010 & sfa == 1
		bys id_mun: egen max = max(mean_ill)
		replace mean_ill = max
		drop max
		
		
***********************************************************
**** 
**** RESTRICT THE SAMPLE * We restrict to the households who are eligible. All are eligible at baseline, but some are not anymore in 2011, 2012.
**** 
***********************************************************		
					
			cap drop ln_expq 
			xtile ln_expq = ln_expenditure if round == 2012 & q1_05 == 1 & sfa == 1, nq(3)
			xtile temp = ln_expenditure if round == 2010 & q1_05 == 1  & sfa == 1, nq(3)
			replace ln_expq = temp if round == 2010 & q1_05 == 1  & sfa == 1
			drop temp
			
			gen inter1 = (mother == 1 & ln_expq == 1) if !mi(ln_expq)
			label var inter1 "Payment to mother * Ln Expenditure (1st tercile)"			
			gen inter2 = (mother == 1 & ln_expq == 2) if !mi(ln_expq)
			label var inter2 "Payment to mother * Ln Expenditure (2nd tercile)"
			gen inter3 = (mother == 1 & ln_expq == 3) if !mi(ln_expq)
			label var inter3 "Payment to mother * Ln Expenditure (3rd tercile)"
			
			gen d1 = (ln_expq == 1) if !mi(ln_expq)
			label var d1 "Ln Expenditure (1st tercile)"			
			gen d2 = (ln_expq == 2) if !mi(ln_expq)
			label var d2 "Ln Expenditure (2nd tercile)"
			gen d3 = (ln_expq == 3) if !mi(ln_expq)
			label var d3 "Ln Expenditure (3rd tercile)"
				
			gen inter_exp = mother * ln_expenditure if !mi(ln_expenditure)
			label var inter_exp "Payment to mother * Ln Expenditure"
		
			
***********************************************************
**** 
**** RESTRICT THE SAMPLE * We restrict to the households who are eligible. All are eligible at baseline, but some are not anymore in 2011, 2012.
**** 
***********************************************************

	gen outsample = (members_15_18 == 0)
	
	*drop if members_15_18 == 0  // Drop if there are no household members in age group 15 - 18
	* we will extend to that we include households with children older than 18 who still qualify
	
	drop if outsample == 1
	
***********************************************************
**** 
**** Select variables for analysis
**** 
***********************************************************

	keep if (round == 2010 | round == 2012 | round == 2013)	& q1_05 == 1 & sfa == 1
	
	keep 	round samplecode id_reg region id_mun municipality sfa q1_05 mother ///
			no_educ_head low_primary_head upp_primary_head sec_school_head more_sec_head gender_head male_adults MAC ALB TUR OTH ROM rel_muslim d_age_head_l35 d_age_head_35 d_age_head_40 d_age_head_50 d_age_head_55 members_2 members_3 members_4 skopje rural members ///
			expenditure* exp_food exp_tobaccoalcohol exp_clothing exp_school exp_health exp_utilities exp_other ///
			exp_food_cereal exp_food_veg exp_food_meat exp_food_fish exp_food_oil exp_food_dairy exp_food_ttea exp_food_ss exp_food_choc exp_food_coffee exp_food_beverages exp_food_outside exp_food_other exp_w_clothing exp_h_clothing exp_ch_clothing exp_school_m exp_school_f exp_school_m_0_14 exp_school_f_0_14 exp_school_m_15 exp_school_f_15 exp_health_w exp_health_h exp_health_ch exp_health_other ///
			ln_expenditure sh_food sh_tobaccoalcohol sh_clothing sh_school sh_health sh_utilities sh_other ///
			sh_tobacco sh_alcohol sh_w_clothing sh_h_clothing sh_ch_clothing sh_school_m sh_school_f sh_school_m_0_14 sh_school_f_0_14 sh_school_m_15 sh_school_f_15 sh_health_w sh_health_h sh_health_ch sh_health_other ///
			ln_expenditure_adj* sh_*food_adj sh_*tobaccoalcohol_adj sh_*clothing_adj sh_*school_adj sh_*health_adj sh_*utilities_adj sh_*other_adj ///
			sh_food_cereal_adj sh_food_veg_adj sh_food_meat_adj sh_food_fish_adj sh_food_oil_adj sh_food_dairy_adj sh_food_ttea_adj sh_food_ss_adj sh_food_choc_adj sh_food_coffee_adj sh_food_beverages_adj sh_food_outside_adj sh_food_other_adj sh_tobacco_adj sh_alcohol_adj sh_w_clothing_adj sh_h_clothing_adj sh_ch_clothing_adj sh_school_m_adj sh_school_f_adj sh_school_m_adj_0_14 sh_school_f_adj_0_14 sh_school_m_adj_15 sh_school_f_adj_15 sh_health_w_adj sh_health_h_adj sh_health_ch_adj sh_health_other_adj ///
			nzero_* panel_2012 members_* sfa_* sfa ///
			sh_sleeping_* sh_chores_* sh_working_* sh_elderly_* sh_shopping_* sh_leisure_ch_* sh_leisure_no_ch_* sh_study_ch_* sh_other_ch_* sh_other_no_ch_* ///
			members* memb_*  ///
			ssh_* sh_food* cct_* ///
			m_present f_present outsample meanmun_* mother_t r_* welfare_center *index* lang_* A1A2 ///
			schooling* mean_* sum_* a1-a4 age_int_cat_s memb memb_* ///
			d1 d2 d3 inter_exp ln_expq inter1 inter2 inter3 income q9_* min_dist_SWC* min_time_SWC*


	save "Dataset/WORKING Macedonia Household survey.dta", replace
